/*
 * StartApplication.java
 *
 * Created on May 28, 2008 6:47:17 PM;
 */

package com.iaroc.irobot.sample;

import com.iaroc.irobot.IRobotCreate;
import com.iaroc.irobot.util.XLights;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.LEDColor;
import com.sun.spot.util.*;

import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class SampleMIDlet extends MIDlet 
    {
    protected IRobotCreate create;
    protected int distFactor = 305084745;
    private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    private EDemoBoard demoBoard = EDemoBoard.getInstance();
    private XLights lights;
    private IAccelerometer3D acc = EDemoBoard.getInstance().getAccelerometer();
    
    protected void doTest() 
        {
        OneMoreEventHandler event = new OneMoreEventHandler(new IRobotCreate(),
                demoBoard);
        while (true)
            event.pollSensors();
//        HomeBaseEventHandler follower = new HomeBaseEventHandler(
//                new IRobotCreate(), EDemoBoard.getInstance());
//        follower.start();
////        // Loop until the play button is pressed
//        while (follower.keepGoing()) 
//            {
//            follower.pollSensors();
//            Utils.sleep(50);
//            }
////        while(true)
//            {            
//            create.spinLeft();
//            create.waitAngle(180);
//            create.stop();
//            Utils.sleep(800);
//            }
        }

    protected void startApp() throws MIDletStateChangeException 
        {
        lights = new XLights(demoBoard, 2, 5);
        lights.startPsilon();
        doTest();
//        FinalHomeBaseEventHandler finalFollower = 
//                new FinalHomeBaseEventHandler(new IRobotCreate(), 
//                EDemoBoard.getInstance());
//        finalFollower.runHome();
//        MaxSonarEZ1 sonar = new MaxSonarEZ1(EDemoBoard.A0);
//        while (true)
//            {
//            System.out.println("Max Sonar: " + sonar.getDistance());
//            Utils.sleep(100);
//            }
        }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        for (int i = 0; i < 8; i++) {
            leds[i].setOff();
        }
    }
}
